package com.ncmmall.mall.controller.securitys;

import com.ncmmall.domain.security.nav.Navigation;
import com.ncmmall.domain.security.user.WebUser;
import com.ncmmall.mall.controller.securitys.shiro.Securitys;
import com.ncmmall.querychannel.service.QueryChannelService;
import com.google.common.collect.ImmutableMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.List;

/**
 * @author likaikai
 *         工作台
 */
@Controller
public class DashboardController {

    @Autowired
    private QueryChannelService queryChannelService;

    /**
     * 后台布局
     */
    @RequestMapping(value = "/dashboard", method = RequestMethod.GET)
    private String dashboard(Model model) {
        List<Navigation> result = queryChannelService.findAll("select n from Navigation n where n.role.id =:id and n.parent is null order by n.rank desc ", ImmutableMap.of("id", Securitys.currentUser().getRole().getId()), Navigation.class);
        model.addAttribute("navigation", result)
                .addAttribute("firstLogin", queryChannelService.findOne(WebUser.class, Securitys.currentUser().getId()).isFirstLogin());;

        return "securitys/dashboard";
    }

    /**
     * 工作台页面
     */
    @RequestMapping(value = "/main", method = RequestMethod.GET)
    private String index(Model model) {

        WebUser merchant = Securitys.currentUser();

        int waitSendOrders = queryChannelService.selectCount("select ifnull(count(1), 0) from mall_order where merchant_id = :id and status = 2 ", ImmutableMap.of("id", merchant.getId()));
        int waitAfterSaleOrders = queryChannelService.selectCount("select ifnull(count(1),0) from mall_order where merchant_id = :id and isAfterSale = 1 ", ImmutableMap.of("id", merchant.getId()));
        int lastDayOrders = queryChannelService.selectCount("select ifnull(count(1),0) from mall_order where merchant_id = :id and payStatus =1 and date_format(createTime,'%Y-%m-%d') = date_format(date_sub(now(),interval 1 day),'%Y-%m-%d') ", ImmutableMap.of("id", merchant.getId()));
        double lastDayOrderPrices = queryChannelService.selectSum("select ifnull(sum(price),0) from mall_order where merchant_id = :id and payStatus =1 and date_format(createTime,'%Y-%m-%d') = date_format(date_sub(now(),interval 1 day),'%Y-%m-%d')  ", ImmutableMap.of("id", merchant.getId()));
        int totalOrders = queryChannelService.selectCount("select ifnull(count(1), 0) from mall_order where merchant_id = :id and payStatus =1 ", ImmutableMap.of("id", merchant.getId()));
        double totalrderPrices = queryChannelService.selectSum("select ifnull(sum(price),0) from mall_order where merchant_id = :id and payStatus =1 ", ImmutableMap.of("id", merchant.getId()));

        model.addAttribute("waitSendOrders", waitSendOrders)
                .addAttribute("waitAfterSaleOrders", waitAfterSaleOrders)
                .addAttribute("balance", merchant.getBalance())
                .addAttribute("lastDayOrders", lastDayOrders)
                .addAttribute("lastDayOrderPrices", lastDayOrderPrices)
                .addAttribute("totalOrders", totalOrders)
                .addAttribute("totalrderPrices", totalrderPrices);

        return "securitys/main";
    }

}
